package top.ahzy.seobench.config;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.ahzy.seobench.filter.FilterInterceptor;
import top.ahzy.seobench.interfaces.requestBodyParam.RequestBodyParamArgumentResolver;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private SeobenchConfig seobenchConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> pathPatterns = new ArrayList<>();
        if(CollUtil.isNotEmpty(seobenchConfig.getPathWhites())){
            pathPatterns = seobenchConfig.getPathWhites();
        }

        pathPatterns.addAll(Arrays.asList("/user/login", "/register","/music/**", "/img/getIcon/**", "/res/**", "/test/**"));
        pathPatterns.addAll(Arrays.asList("/doc.html", "/webjars/**", "/favicon.ico", "/swagger-resources", "/v2/api-docs"));
        registry.addInterceptor(new FilterInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(pathPatterns);

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(seobenchConfig.getGetfaviconRes()+"**")
                .addResourceLocations("file:/"+ seobenchConfig.getGetfaviconStorage());
    }

    /**
     * 注册自定义参数解析器
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new RequestBodyParamArgumentResolver());
    }

}

